from flask_wtf import FlaskForm
from wtforms import StringField,TextAreaField,SubmitField,SelectField
from wtforms.validators import DataRequired,Length,ValidationError


def validate_content(form,field):
    value = field.data
    if len(value) <= 50:
        raise ValidationError("新闻内容长度不能少于50个字符2")
    return field

class NewsForm_mysql(FlaskForm):
    # 新闻表单
    # title = StringField(label="新闻标题",validators=[DataRequired("请输入标题"),Length(min=10,max=20,message='新闻标题的长度在20-200之间')],description="请输入标题",render_kw={"required": "required", "class": "form-control"})
    # content = TextAreaField(label='新闻内容', validators=[DataRequired("请输入内容"),validate_content],
    #                         description="请输入内容",
    #                         render_kw={"required": "required", "class": "form-control"})
    title = StringField(label="新闻标题", validators=[DataRequired("请输入标题")],
                        description="请输入标题", render_kw={"required": "required", "class": "form-control"})
    content = TextAreaField(label='新闻内容', validators=[DataRequired("请输入内容")],
                            description="请输入内容",
                            render_kw={"required": "required", "class": "form-control"})
    news_type = SelectField('新闻类型',
                        choices=[('本地', '本地'), ('百家', '百家'), ('娱乐', '娱乐'), ('军事', '军事')],
                        render_kw={'class': 'form-control'})
    img_url = StringField(label='新闻图片',
                        description='请输入图片地址',
                          default='/static/img/news/new1.jpg',
                        render_kw={'class': 'form-control'})
    # is_top = BooleanField(label='是否置顶')
    submit = SubmitField('提交')

    # # 自动调取
    # def validate_content(self, field):
    #     value = field.data
    #     if len(value) <= 50:
    #         raise ValidationError("新闻内容长度不能少于50个字符")
    #     return field
